<?php
defined('IN_SITE') or exit('Access Denied');

require_once 'wiki.class.php';
$wiki = new wiki();
require_once MOD_ROOT.'attachment/attachment.class.php';
$attachment = new attachment($module, $catid);

// 关于PHP表单提交错误后返回内容消失问题
header("Cache-control: private");
switch($action)
{
    default:
		
		if($wikiid){
			// 如果参数是自定义名称
			if(!$wikiid && $prefix){
				$result = $db->get_one("select `wikiid` from ".DB_PRE."wiki where prefix = '$prefix'");
				$wikiid = $result['wikiid'];
				unset($result);
			} 
			
			// 主要获取该文章的栏目所在信息，例如详细页模板等等
			require_once 'wiki.class.php';
			$wiki = new wiki();
			$r = $wiki->get($wikiid);
			
			// 获取该文章的所有内容信息
			require_once 'model/wiki_output.class.php';
			$out = new wiki_output();
			$data = $out->get($r);
			extract($data);
		
			// 设置文章的头部信息、关键字等
			$title = strip_tags($title);
			$head['title'] = $title.'_'.$SETTING['sitename'];
			$head['keywords'] = str_replace(' ', ',', $r['keywords']);
			$head['description'] = $r['description'];
			 
			
			header('Last-Modified: '.gmdate('D, d M Y H:i:s', $r['updatetime']).' GMT');
			header('Expires: '.gmdate('D, d M Y H:i:s', $r['updatetime']+CACHE_PAGE_CONTENT_TTL).' GMT');
			header('Cache-Control: max-age='.CACHE_PAGE_CONTENT_TTL.', must-revalidate');
			
			include template('view.html','wiki');
			cache_page(CACHE_PAGE_CONTENT_TTL);
		}
		else{
			$head['title'] = '_'.$SETTING['sitename'];
			$head['keywords'] = $meta_keywords;
			$head['description'] = $meta_description;
			
			$ttl = $child == 1 ? CACHE_PAGE_CATEGORY_TTL : CACHE_PAGE_LIST_TTL;
			header('Last-Modified: '.gmdate('D, d M Y H:i:s', TIME).' GMT');
			header('Expires: '.gmdate('D, d M Y H:i:s', TIME + $ttl).' GMT');
			header('Cache-Control: max-age='.$ttl.', must-revalidate');
			include template('index.html','wiki');
		}
	break; 
	
	case 'tag':
		$keyword = load('keyword.class.php','keyword');
		$data = $keyword->get($tag);
		if(!$data) showmessage("TAG: $tag 不存在！");
		$keyword->hits($tag);
		extract($data);
		
		// 所有tag标签连成的字符串，用于sql查询的时候where in()
		$data2 = $db->select("select `tag` from table_wiki_tag t");
		foreach($data2 as $k=>$v){
			$tags .= !$tags ? $v['tag'] : ','.$v['tag'];
		}
		
		$head['title'] = $tag.'-'.$SETTING['sitename'];
		
		include template('tag.html');
	break;
	
	case 'tags':
	
	break;
	
	case 'search':
	
	break;
	
	case 'create':
		if($dosubmit)
		{
			$info['catid'] = $catid;
			$info['status'] = 99;
			if(isset($info['inputtime'])) $info['updatetime'] = $info['inputtime'];
			$wikiid = $wiki->add($info,$cat_selected);
			if($wikiid) showmessage('发布成功！', url("?module=$module"));
		}
		else{
			$modelid = 55;
			require_once 'form.class.php';
			require 'model/wiki_form.class.php';
			$wiki_form = new wiki_form($modelid);
			/*$data['catid'] = $catid;*/
			$forminfos = $wiki_form->get();
			
			//dump($forminfos);
			include template('create.html','wiki');
		}
	break;
	
	case 'edit':
	
	break;
	
	case 'delete':
	
	break;
	
	case 'mine':
		include template('mine.html','wiki');
	break;
	
	case 'rss':
		require MOD_ROOT.'rss/rss.class.php';
			
		$encoding   =  CHARSET;
		$about      =  SITE_URL;
		$title      =  '百科 -'.$SETTING['sitename'];
		$description = '描述';
		$image_link =  '缩略图';
		$category   =  '';
		$cache      =  60;
		$rssfile    = new RSSBuilder($encoding, $about, $title, $description, $image_link, $category, $cache);
		$publisher  =  '';
		$creator    =  SITE_URL;
		$date       =  date('r');
		$rssfile->addDCdata($publisher, $creator, $date);
		$period     =  '';
		$frequency =  SITE_URL;
		$base =  date('Y');
		$rssfile->addSYdata($period, $frequency, $base);
		$sql = "SELECT `title`, `description`, `url`, `inputtime`, `thumb`, `keywords` FROM $wiki->table WHERE `status` = '99' ORDER BY `inputtime` DESC LIMIT 0 , 20";
		$result = $db->query($sql);
		
		while ( $r = $db->fetch_array($result) )
		{
			
			$img = $r['thumb'] ? "<img src=".thumb($r['thumb'], 150, 150)." border='0' /><br />" : '';
			$tags = $r['keywords'] ? $r['keywords'] : '';
			$about          =  $link = url($r['url']); // url链接不要含连字符&
			$title          =  "<![CDATA[".$r['title']."]]>";
			$description    =  "<![CDATA[".$img.$r['description'].$tags."]]> ";
			$subject        =  '';
			$date           =  date('Y-m-d H:i:s' , $r['inputtime']);
			$author         =  $SETTING['sitename'].' '.SITE_URL;
			$comments       =  '';//注释
			$image          =  '';
			$rssfile->addItem($about, $title, $link, $description, $subject, $date,	$author, $comments, $image);
		}
		$version = '2.00';
		$rssfile->outputRSS($version);
	break;
	
	// 点击率，调用方法
	//<script language="JavaScript" src="?module=codex&action=count&codexid={$codexid}">...
	case 'count':
		if($wikiid)
		{
			$wikiid = intval($wikiid);
			$r = $db->get_one("SELECT * FROM `".DB_PRE."wiki` WHERE `wikiid`=$wikiid");
			if(!$r) return false;
			$hits = $r['hits'] + 1;
			return $db->query("UPDATE `".DB_PRE."wiki` SET `hits`=$hits WHERE `wikiid`=$wikiid");
			
		}
		echo "\$('#hits').html($hits)";
	break;
	
	
}
?>